package com.example.myapplication

import android.graphics.Color
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import java.text.SimpleDateFormat
import java.util.*

class StatisticsActivity : AppCompatActivity() {
    
    private lateinit var textTotalPatients: TextView
    private lateinit var textTotalUsers: TextView
    private lateinit var textTotalDepartments: TextView
    private lateinit var textTodayPatients: TextView
    private lateinit var textWeekPatients: TextView
    private lateinit var textMonthPatients: TextView
    private lateinit var listViewDepartmentStats: ListView
    private lateinit var listViewUserStats: ListView
    private lateinit var databaseHelper: DatabaseHelper
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_statistics)
        
        databaseHelper = DatabaseHelper(this)
        
        initViews()
        loadStatistics()
    }
    
    private fun initViews() {
        textTotalPatients = findViewById(R.id.textTotalPatients)
        textTotalUsers = findViewById(R.id.textTotalUsers)
        textTotalDepartments = findViewById(R.id.textTotalDepartments)
        textTodayPatients = findViewById(R.id.textTodayPatients)
        textWeekPatients = findViewById(R.id.textWeekPatients)
        textMonthPatients = findViewById(R.id.textMonthPatients)
        listViewDepartmentStats = findViewById(R.id.listViewDepartmentStats)
        listViewUserStats = findViewById(R.id.listViewUserStats)
    }
    
    private fun loadStatistics() {
        val currentUser = UserSession.currentUser
        if (currentUser == null) return
        
        val patients = databaseHelper.getPatientsByUserType(currentUser)
        val users = databaseHelper.getAllUsers()
        val departments = databaseHelper.getAllDepartments()
        
        // 基础统计
        textTotalPatients.text = "总患者数: ${patients.size}"
        textTotalUsers.text = "总用户数: ${users.size}"
        textTotalDepartments.text = "总科室数: ${departments.size}"
        
        // 时间统计
        val now = Date()
        val calendar = Calendar.getInstance()
        
        // 今日患者数
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        val todayStart = calendar.time
        val todayPatients = patients.count { it.reportTime.after(todayStart) }
        textTodayPatients.text = "今日患者: $todayPatients"
        
        // 本周患者数
        calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 1))
        val weekStart = calendar.time
        val weekPatients = patients.count { it.reportTime.after(weekStart) }
        textWeekPatients.text = "本周患者: $weekPatients"
        
        // 本月患者数
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        val monthStart = calendar.time
        val monthPatients = patients.count { it.reportTime.after(monthStart) }
        textMonthPatients.text = "本月患者: $monthPatients"
        
        // 科室统计
        loadDepartmentStatistics(patients, users)
        
        // 用户统计
        loadUserStatistics(patients, users)
    }
    
    private fun loadDepartmentStatistics(patients: List<Patient>, users: List<User>) {
        val departmentStats = mutableMapOf<String, Int>()
        
        // 统计各科室患者数
        patients.forEach { patient ->
            val count = departmentStats[patient.department] ?: 0
            departmentStats[patient.department] = count + 1
        }
        
        val statsList = departmentStats.map { (dept, count) ->
            "科室: $dept - 患者数: $count"
        }
        
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, statsList)
        listViewDepartmentStats.adapter = adapter
    }
    
    private fun loadUserStatistics(patients: List<Patient>, users: List<User>) {
        val userStats = mutableMapOf<String, Int>()
        
        // 统计各用户上报的患者数
        patients.forEach { patient ->
            val user = users.find { it.id == patient.reportedBy }
            val userName = user?.realName ?: "未知用户"
            val count = userStats[userName] ?: 0
            userStats[userName] = count + 1
        }
        
        val statsList = userStats.map { (user, count) ->
            "用户: $user - 上报数: $count"
        }
        
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, statsList)
        listViewUserStats.adapter = adapter
    }
} 